package Pages;

import java.util.*;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

import clientMain.*;

/**
 * Login Page
 * @author Andrew Fox
 *
 */

public class LoginPage extends Page{


	public LoginPage(Page page){
//		super(page.url,page.lastURL,page.client);
//		this.html=page.html;
//		this.pageType=page.pageType;
		super(page.url,page.html,page.client,page.pageType);
		this.pageOpenTime=page.pageOpenTime;
		this.lastURL=page.lastURL;
	}

	/**
	 * Determines the next link to go to in the application
	 * @return the URL or HTML of the next link, depending on whether
	 * 		   the previous entry contained a form or not
	 */
	public StringBuffer makeDecision() throws Exception{
		HashMap<String, StringBuffer> data = new HashMap<String, StringBuffer>();
		StringBuffer nextURL=new StringBuffer();

		if (client.loginAttempts<3){
			StringBuffer userID=new StringBuffer();
			if (HTML4==false){
				ObjectMapper mapper = new ObjectMapper();
				if (html.charAt(0)!='{')
					html=html.insert(0,"{");
				if(verbose)System.out.println(html);
				JsonNode node = mapper.readValue(html.toString(), JsonNode.class);
				userID=new StringBuffer(node.get("userID").getValueAsText());
			}

			if(HTML4||userID.equals("-1")){
				data.put("username", typingError(client.userInfo.username));
				data.put("password", typingError(client.userInfo.password));
				client.loginAttempts+=1;

				// Think Time
				try{Thread.sleep(getThinkTime());}
				catch(InterruptedException e){
					e.printStackTrace();
				}
				if (HTML4)
					//nextURL=this.doSubmit2(this.url,data,"login?");
						nextURL=doSubmit3(new StringBuffer(runSettings.rootURL).append("/login?"),data,client);
				else
					nextURL=new StringBuffer(runSettings.rootURL).append("/login?").append(createURL(data));
				
				if (HTML4&&nextURL.indexOf("<HEAD><TITLE>Login</TITLE></HEAD>")==-1){
					client.loggedIn=true;
				}
				else if(HTML4&&nextURL.indexOf("<HEAD><TITLE>Login</TITLE></HEAD>")!=-1){
					nextURL=new StringBuffer(runSettings.rootURL).append("/login");
				}
			}
			else{
				client.userInfo.userID=userID;
				nextURL=new StringBuffer(runSettings.rootURL).append("/myaccount");
			}

		}
		else{
			client.exit=true;
		}

		return nextURL;
	}

	/**
	 * Determines the think time before going to the next page
	 * Deducts from the think time the amount of time since the 
	 * page opened until this calculation is performed
	 * @return Think time in ms
	 */
	public int getThinkTime(){
		int thinkTime=(int)expDist(initialThinkTime);

		thinkTime+=(int)(((client.userInfo.username.length()+client.userInfo.password.length())/client.typingSpeed))*client.loginAttempts;
		if (verbose)System.out.println("User: "+client.userInfo.username+" - Think Time: "+thinkTime+" ms");
		return (int) ((thinkTime-(new Date().getTime()-pageOpenTime))/runSettings.thinkTimeSpeedUpFactor);
	}


}
